package com.example.wisdomclassroomapi.facade.controller.api;

import com.example.wisdomclassroomapi.application.service.AuthRoleApplicationService;
import com.example.wisdomclassroomapi.application.service.AuthUserApplicationService;
import com.example.wisdomclassroomapi.common.util.IdentificationUtil;
import com.example.wisdomclassroomapi.domain.entity.AuthRoleEntity;
import com.example.wisdomclassroomapi.domain.entity.AuthUserEntity;
import com.example.wisdomclassroomapi.domain.service.CaptchaDomainService;
import org.apache.ibatis.annotations.Param;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    @Resource
    private CaptchaDomainService captchaDomainService;
    @Resource
    private AuthUserApplicationService authUserApplicationService;
    @Resource
    private AuthRoleApplicationService authRoleApplicationService;
    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 获取验证码
     * @param captchaId 验证码ID
     * @return Map<String, Object>
     * @throws IOException IOException
     */
    @PostMapping("/captcha")
    public Map<String, Object> captcha(@Param("captchaId") String captchaId) throws IOException {
        return captchaDomainService.createCaptcha(captchaId);
    }

    /**
     * 注册
     * @param params Map<String, String>
     */
    @PostMapping("/register")
    public String register(@RequestBody Map<String, String> params) {
        captchaDomainService.checkCaptcha(params.get("captchaId"), params.get("captcha"));
        String id = authUserApplicationService.add(params);

        // 绑定学生角色
        AuthRoleEntity entity = new AuthRoleEntity();
        entity.setType("0");
        List<String> ids = authRoleApplicationService.listRoleId(entity);
        authUserApplicationService.bindRole(id, ids);

        return id;
    }

    @PostMapping("/reset/sendEmailCode")
    public String sendEmailCode(@RequestBody Map<String, String> params) {
        captchaDomainService.checkCaptcha(params.get("captchaId"), params.get("captcha"));
        String email = params.get("email");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("课程点播系统-重置密码");
        message.setFrom("luyeplus@foxmail.com");
        message.setTo(email);
        message.setSentDate(new Date());

        String mailCodeId = IdentificationUtil.shortUuid();
        String captcha = captchaDomainService.createCode(mailCodeId);
        message.setText("您正在进行密码重置操作，所需要的邮箱验证码为" + captcha);
        javaMailSender.send(message);
        return mailCodeId;
    }

    @PostMapping("/reset/checkEmailCode")
    public void checkEmailCode(@RequestBody Map<String, String> params) {
        captchaDomainService.checkCaptcha(params.get("captchaId"), params.get("captcha"));
        AuthUserEntity entity = new AuthUserEntity();
        entity.setEmail(params.get("email"));
        List<AuthUserEntity> list = authUserApplicationService.list(entity);
        entity = list.get(0);
        entity.setPassword(passwordEncoder.encode(params.get("password")));
        authUserApplicationService.updateById(entity);
    }
}
